/*
 * The MIT License
 *
 * Copyright 2015 usuario.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.chronoserp.controller;

import com.chronoserp.modulo.estoque.controller.CadAlmoxarifadoController;
import com.chronoserp.modulo.estoque.view.ConsAlmoxarifado;
import com.chronoserp.entidades.Almoxarifado;
import com.chronoserp.entidades.Usuario;
import com.chronoserp.modulo.estoque.servidor.ConsAlmoxarifadoAction;
import com.chronoserp.padrao.cliente.Container;
import com.chronoserp.padrao.java.Constantes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.openswing.swing.client.GridControl;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.ValueObject;
import org.openswing.swing.message.send.java.GridParams;
import org.openswing.swing.table.client.GridController;
import org.openswing.swing.table.java.GridDataLocator;
import org.openswing.swing.util.client.ClientUtils;

/**
 *
 * @author usuario
 */
public class AConsController extends GridController implements GridDataLocator {

    private ConsAlmoxarifado grid;
    private String acaoServidor;

    public AConsController() {
//        this.grid = new ConsAlmoxarifado(this);
        this.acaoServidor = "consAlmoxarifadoAction";
        MDIFrame.add(grid);
    }

    @Override
    public Response loadData(int action, int startIndex, Map filteredColumns, ArrayList currentSortedColumns, ArrayList currentSortedVersusColumns, Class valueObjectType, Map otherGridParams) {
        //define os parametros da grid
        otherGridParams.put("acao", Constantes.LOAD);
        if (Container.isTresCamadas()) {
            return ClientUtils.getData(acaoServidor, new GridParams(action, startIndex, filteredColumns, currentSortedColumns, currentSortedVersusColumns, otherGridParams));
        } else {
            ConsAlmoxarifadoAction acao = new ConsAlmoxarifadoAction();
            return acao.executeCommand(new GridParams(action, startIndex, filteredColumns, currentSortedColumns, currentSortedVersusColumns, otherGridParams), null, null, null, null, null);
        }
    }

    @Override
    public Response deleteRecords(ArrayList persistentObjects) throws Exception {
        //define os parametros da grid
        Map otherGridParams = new HashMap();
        otherGridParams.put("acao", Constantes.DELETE);
        otherGridParams.put("persistentObjects", persistentObjects);
        Usuario usr = (Usuario) Container.getContainer().get("usuario");
        otherGridParams.put("usuario", usr);
        //seta os parametros da grid
        GridParams pars = new GridParams(0, 0, null, null, null, otherGridParams);
       
        if (Container.isTresCamadas()) {
            return ClientUtils.getData(acaoServidor, pars);
        } else {
            ConsAlmoxarifadoAction acao = new ConsAlmoxarifadoAction();
            return acao.executeCommand(pars, null, null, null, null, null);
        }
    }

    @Override
    public void doubleClick(int rowNumber, ValueObject persistentObject) {
        Almoxarifado banco = (Almoxarifado) persistentObject;
        new CadAlmoxarifadoController(grid, banco.getId().toString());

    }

    @Override
    public boolean beforeInsertGrid(GridControl grid) {
        new CadAlmoxarifadoController(this.grid, null);
        return false;
    }

}
